package com.dtt.company.project.service;

import java.util.List;

import com.dtt.company.project.dao.ProjectDao;
import com.dtt.company.project.model.ProjectModel;
import com.dtt.company.util.FmtEmpty;

public class ProjectService {
	private ProjectDao dao=new ProjectDao();

	public String insert(ProjectModel model) {
		//添加的逻辑主键（账号）存在时，不允许添加
		ProjectModel mdb=selectModel(model);
		if(mdb==null) { 
			Integer result=dao.insert(model);
			return String.valueOf(result);
		} 
		return "E";
	}

	public String delete(ProjectModel model) {
		Integer result=dao.delete(model);
		return String.valueOf(result);
	}

	public String update(ProjectModel model) {
		Integer result=dao.update(model);
		return String.valueOf(result);
	}

	/**
	 *多条件模糊查询出一些记录
	 * @author Dtt
	 * @return
	 */
	public List<ProjectModel> select(ProjectModel model) {
		String code=model.getCode();
//		System.out.println("code==="+code);
		if(code!=null)
			model.setCode("%"+code+"%") ;
		String name=model.getName();
//		System.out.println("name==="+name);
		if(name!=null)
			model.setName("%"+name+"%");
		return dao.select(model);
	}

	/**
	 * 根据逻辑主键查询出唯一的记录，主键不存在返回null
	 * @author Dtt
	 * @return
	 */
	public ProjectModel selectModel(ProjectModel model) {
		ProjectModel temp=new ProjectModel(model.getCode());
		List<ProjectModel> list = dao.select(temp);
		if(FmtEmpty.isEmpty(list)) {
			return null;
		}
		return list.get(0);
		
	}
	//分页：根据多条件的模糊查询的数据总数
	public int selectCount(ProjectModel model) {
		ProjectModel mdb=new ProjectModel();
		String code=model.getCode();
		mdb.setCode(code == null ? "%%":"%"+code+"%");
		String name=model.getName();
		mdb.setName(name == null ? "%%":"%"+name+"%");
		mdb.setPageOn(false);
		return dao.selectCount(mdb);
		
	}
}
